<template>
  <el-dialog
    :title="title==='edit' ? '修改商品类型': '新增商品类型'"
    :visible="dialogVisible"
    @close="handleClose"
  >
    <el-form ref="form" label-width="140px" :rules="rules" :model="form">
      <el-form-item label="商品类型名称：" prop="className">
        <el-input
          v-model="form.className"
          type="text"
          style="width:396px"
          show-word-limit
          placeholder="请输入"
          maxlength="10"
          :validate-event="false"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button style="background-color: #fbf4f0!important;color: #655b56!important;border:0;width:80px;height:36px;padding:0" @click="handleClose">取 消</el-button>
      <el-button type="primary" style="background: linear-gradient(135deg,#ff9743,#ff5e20)!important;border:0;width:80px;height:36px;padding:0;margin:0 0 0 34px" @click="addSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addGoodsType, changeGoodsType } from '@/api/goods'
export default {
  name: 'DiagLog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: null
    },
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {
        className: '',
        classId: null
      },
      rules: {
        className: [{ required: true, message: '请输入' }]
      }
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('handleClose', false)
    },
    async addSubmit() {
      try {
        await this.$refs.form.validate()
        console.log(this.title)
        if (this.title === 'add') {
          const res = await addGoodsType(this.form)
          console.log(res)
          this.handleClose()
          this.$message.success('添加成功')
          this.$emit('initTableData')
        } else {
          await changeGoodsType(this.form)
          this.handleClose()
          this.$message.success('修改成功')
          this.$emit('initTableData')
        }
      } catch (error) {
        console.log(error)
      }
    },
    changeGoodsType(row) {
      this.form.className = row.className
      this.form.classId = row.classId
    }
  }
}
</script>

<style scoped lang='less'>
/deep/.el-dialog{
    border-radius: 10px;
    width: 630px;
    .el-dialog__header{
        .el-dialog__title{
            font-weight:700;
        }
    }
    .el-dialog__body{
    padding: 20px 20px 30px;
    text-align: center;
     .el-form{
       .el-form-item{
        text-align: left;
         label{
                height: 36px;
                font-size: 14px;
                font-weight: normal;
                color: #606266;
            }
        .el-form-item__content{
            .el-input{
                .el-input__inner{
                    height: 36px;
                }
            }
        }
        }
     }
    }
    .el-dialog__footer{
      text-align: center;
    }
}

</style>
